package study.saas.order.service;


import org.springframework.stereotype.Service;
import study.saas.common.entity.YPageable;
import study.saas.common.repo.Repo;
import study.saas.common.util.CopyClassUtil;
import study.saas.common.util.PageUtils;
import study.saas.common.util.StringUtil;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.common.viewmodel.PageModel;
import study.saas.order.entity.Orderr;
import study.saas.order.repo.OrderRepo;
import study.saas.order.service.model.OrderCreateDTO;
import study.saas.order.service.model.OrderFilterDTO;
import study.saas.order.service.model.OrderModifyDTO;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class OrderService {

    @Resource
    private OrderRepo orderRepo;


    public ApiResultV2<Orderr> get(Integer orderId) {
        Orderr order = orderRepo.findFistByOrderId(orderId);
        if (order!= null && order.isDelete()) {
            order = null;
        }
        return ApiResultV2.ok(order);

    }

    public ApiResultV2 create(OrderCreateDTO create) {
        Orderr order = orderRepo.findFistByOrderId(create.getOrderId());
        if (order != null && !order.isDelete()) {
            return ApiResultV2.error("订单重复");
        }
        Orderr newOrder;
        if (order == null) {
            newOrder = new Orderr();
        } else {
            newOrder = order;
            newOrder.setIsDeleted(0);
        }
        CopyClassUtil.copyClass(create, newOrder);
        orderRepo.save(newOrder);
        return ApiResultV2.ok();
    }

    public ApiResultV2 modify(OrderModifyDTO modify) {
        Orderr order = orderRepo.findFistByOrderId(modify.getOrderId());
        if (order == null || order.isDelete()) {
            return ApiResultV2.error("订单不存在");
        }
        CopyClassUtil.copyClass(modify, order);
        orderRepo.save(order);
        return ApiResultV2.ok();
    }

    public ApiResultV2 delete(Integer orderId) {
        Orderr order = orderRepo.findFistByOrderId(orderId);
        if (order == null || order.isDelete()) {
            return ApiResultV2.ok();
        }
        order.setIsDeleted(1);
        orderRepo.save(order);
        return ApiResultV2.ok();
    }

    public ApiResultV2<PageModel<Orderr>> list(OrderFilterDTO filter) {
        YPageable pageable = PageUtils.Ypageable(filter.getPage(), filter.getPageSize());
        StringBuilder sql = new StringBuilder();
        Map<String, Object> param = new HashMap<>();
        sql.append(" select e from Orderr e where e.isDeleted = 0 ");
        if (!StringUtil.isEmpty(filter.getNameE())) {
            sql.append(" and e.name = :getNameE");
            param.put("getNameE", filter.getNameE());
        }
        if (!StringUtil.isEmpty(filter.getNameL())) {
            sql.append(" and e.name like :getNameL");
            param.put("getNameL", "%" + filter.getNameL() + "%");
        }
        sql.append(" order by e.createTime desc");
        pageable.setCountSql(Repo.toCountJPql(sql.toString()));
        PageModel<Orderr> pageModel = orderRepo.queryPage(sql.toString(), param, Orderr.class, pageable);
        return ApiResultV2.ok(pageModel);
    }

}
